//
//  MenuListViewController.m
//  YunGui
//
//  Created by wmm on 15/11/5.
//  Copyright © 2015年 hanen. All rights reserved.
//

#import "MenuListViewController.h"
#import "SideMenuUtil.h"
#import "YGNavigationController.h"
#import "HomeViewController.h"
#import "PropertyViewController.h"
#import "ViewController.h"
#import "CabinetCheckController.h"
#import "SubDistrictController.h"
#import "MAMapViewController.h"
#import "SettingViewController.h"
#import "VisitViewController.h"
#import "TaiZhangManagerViewController.h"
#import "NetStatisticViewController.h"

#define kSidebarCellTextKey	@"CellText"
#define kSidebarCellImageKey	@"CellImage"
#define kSidebarCellSelectImageKey	@"CellSelectImage"
#define kGHRevealSidebarWidth2 [UIView getWidth:150.0f]

@interface MenuListViewController (){
    NSArray *_cellInfos;	//!< 单元格信息.
    NSArray *_controllers;	//!< 导航控制器集.
    PropertyViewController *proVC;
}
@property (strong, nonatomic) UITableView *menuTableView;
@property (strong, nonatomic) UIButton *headImgBtn;
@property (strong, nonatomic) UILabel  *workNumLbl;
@property (strong, nonatomic) UIButton *logoutBtn;

@property (strong, nonatomic) UIView *overlayView;
@property (strong,nonatomic) UIImagePickerController * imagePikerViewController;
@property (strong,nonatomic) UIButton *takeButton;
@property (strong,nonatomic) UIImageView* middleView;
//@property (strong,nonatomic) CreatePropertyViewController *createPropertyViewController;

@end

@implementation MenuListViewController

@synthesize revealController;
@synthesize roleId;

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置自身窗口尺寸
    self.view.frame = CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth2, CGRectGetHeight(self.view.bounds));
    self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, kGHRevealSidebarWidth2, CGRectGetHeight(self.view.bounds))];
    bg.image = [UIImage imageNamed:@"CebianBj.png"];
    [self.view addSubview:bg];
    
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    // 绑定主页为内容视图.
//    if (YES) {
//
//        YGNavigationController *ygNV = [[YGNavigationController alloc]initWithRootViewController:homeVC];
//        [SideMenuUtil addNavigationGesture:ygNV revealController:revealController];
//        [SideMenuUtil setRevealControllerProperty:ygNV revealController:revealController];
//        revealController.contentViewController = ygNV;
//    }
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    int userRole = [[userDefaultes objectForKey:@"roleId"] intValue];
    
    SubDistrictController *subVC = [[SubDistrictController alloc] init];
    proVC = [[PropertyViewController alloc] init];
    MAMapViewController *mapVC = [[MAMapViewController alloc] init];

    CabinetCheckController *cabinetVC = [[CabinetCheckController alloc] init];
    
    SettingViewController *settingVC = [[SettingViewController alloc] init];

    VisitViewController *visitVC = [[VisitViewController alloc] init];
    TaiZhangManagerViewController *taiZhangVC = [[TaiZhangManagerViewController alloc] init];
    NetStatisticViewController *netVC = [[NetStatisticViewController alloc] init];
    
    //管理层
    if(userRole == 1){
        
        YGNavigationController *ygNV = [[YGNavigationController alloc]initWithRootViewController:visitVC];
        [SideMenuUtil addNavigationGesture:ygNV revealController:revealController];
        [SideMenuUtil setRevealControllerProperty:ygNV revealController:revealController];
        revealController.contentViewController = ygNV;
        _cellInfos = @[
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Baifang.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Baifang.png"], kSidebarCellTextKey: NSLocalizedString(@"拜访管理", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Xiaoqu.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Xiaoqu.png"], kSidebarCellTextKey: NSLocalizedString(@"小区管理", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Taizhang.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Taizhang.png"], kSidebarCellTextKey: NSLocalizedString(@"台账管理", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Xiaoqu.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Xiaoqu.png"], kSidebarCellTextKey: NSLocalizedString(@"网点统计", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Shezhi.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Shezhi.png"], kSidebarCellTextKey: NSLocalizedString(@"设置", @"")}
                       ];
        _controllers = @[
                         [[YGNavigationController alloc]initWithRootViewController:visitVC],
                         [[YGNavigationController alloc]initWithRootViewController:subVC],
                         [[YGNavigationController alloc]initWithRootViewController:taiZhangVC],//台账
                         [[YGNavigationController alloc]initWithRootViewController:netVC],//网点
                         [[YGNavigationController alloc]initWithRootViewController:settingVC]
                         ];
    }else if(userRole == 2){
        YGNavigationController *ygNV = [[YGNavigationController alloc]initWithRootViewController:visitVC];
        [SideMenuUtil addNavigationGesture:ygNV revealController:revealController];
        [SideMenuUtil setRevealControllerProperty:ygNV revealController:revealController];
        revealController.contentViewController = ygNV;
        _cellInfos = @[
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Baifang.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Baifang.png"], kSidebarCellTextKey: NSLocalizedString(@"拜访管理", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Wuyeguanli.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Wuye.png"], kSidebarCellTextKey: NSLocalizedString(@"物业管理", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Xiaoqu.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Xiaoqu.png"], kSidebarCellTextKey: NSLocalizedString(@"小区管理", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Mingpian.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Mingpian.png"], kSidebarCellTextKey: NSLocalizedString(@"名片扫描", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Taizhang.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Taizhang.png"], kSidebarCellTextKey: NSLocalizedString(@"台账管理", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Ditu.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Ditu.png"], kSidebarCellTextKey: NSLocalizedString(@"小区地图", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Shezhi.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Shezhi.png"], kSidebarCellTextKey: NSLocalizedString(@"设置", @"")}
                       ];
        _controllers = @[
                         [[YGNavigationController alloc]initWithRootViewController:visitVC],
                         [[YGNavigationController alloc]initWithRootViewController:proVC],
                         [[YGNavigationController alloc]initWithRootViewController:subVC],
                         @"scanning",
                         [[YGNavigationController alloc]initWithRootViewController:taiZhangVC],//台账
                         [[YGNavigationController alloc]initWithRootViewController:mapVC],
                         [[YGNavigationController alloc]initWithRootViewController:settingVC]
                         ];
        
    }else if(userRole == 4){//商务经理
        YGNavigationController *ygNV = [[YGNavigationController alloc]initWithRootViewController:homeVC];
        [SideMenuUtil addNavigationGesture:ygNV revealController:revealController];
        [SideMenuUtil setRevealControllerProperty:ygNV revealController:revealController];
        revealController.contentViewController = ygNV;
        _cellInfos = @[
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Baifang.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Baifang.png"], kSidebarCellTextKey: NSLocalizedString(@"我的拜访", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Wuyeguanli.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Wuye.png"], kSidebarCellTextKey: NSLocalizedString(@"物业管理", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Xiaoqu.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Xiaoqu.png"], kSidebarCellTextKey: NSLocalizedString(@"小区管理", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Mingpian.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Mingpian.png"], kSidebarCellTextKey: NSLocalizedString(@"名片扫描", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Ditu.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Ditu.png"], kSidebarCellTextKey: NSLocalizedString(@"小区地图", @"")},
                        @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Shezhi.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Shezhi.png"], kSidebarCellTextKey: NSLocalizedString(@"设置", @"")}
        ];
        _controllers = @[
                         [[YGNavigationController alloc]initWithRootViewController:homeVC],
                         [[YGNavigationController alloc]initWithRootViewController:proVC],
                         [[YGNavigationController alloc]initWithRootViewController:subVC],
                         @"scanning",
                         [[YGNavigationController alloc]initWithRootViewController:mapVC],
                         [[YGNavigationController alloc]initWithRootViewController:settingVC]
                         ];
    }else{
        YGNavigationController *ygNV = [[YGNavigationController alloc]initWithRootViewController:cabinetVC];
        [SideMenuUtil addNavigationGesture:ygNV revealController:revealController];
        [SideMenuUtil setRevealControllerProperty:ygNV revealController:revealController];
        revealController.contentViewController = ygNV;
        _cellInfos = @[
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Guiti.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Guiti.png"], kSidebarCellTextKey: NSLocalizedString(@"柜体验收", @"")},
                       @{kSidebarCellImageKey: [UIImage imageNamed:@"Btn_Normal_Shezhi.png"], kSidebarCellSelectImageKey: [UIImage imageNamed:@"Btn_Push_Shezhi.png"], kSidebarCellTextKey: NSLocalizedString(@"设置", @"")}
                       ];
        _controllers = @[
                         [[YGNavigationController alloc]initWithRootViewController:cabinetVC],
                         [[YGNavigationController alloc]initWithRootViewController:settingVC]
                         ];
    }
    NSLog(@"userRole:%d",userRole);
    
    // 添加手势.
    for (id obj in _controllers) {
        [SideMenuUtil setRevealControllerProperty:obj revealController:revealController];
        if ([obj isKindOfClass:UINavigationController.class]) {
            [SideMenuUtil addNavigationGesture:(UINavigationController*)obj revealController:revealController];
        }
    }
    
    // ui.
    [self createUI];
    
    self.imagePikerViewController = [[UIImagePickerController alloc] init];
    self.imagePikerViewController.delegate = self;
    self.imagePikerViewController.allowsEditing = YES;
}

- (void)viewDidUnload {
    [self setMenuTableView:nil];
    [super viewDidUnload];
}

- (void)createUI{
    UIColor *bgColor = [UIColor colorWithRed:(50.0f/255.0f) green:(57.0f/255.0f) blue:(74.0f/255.0f) alpha:1.0f];
    self.view.backgroundColor = bgColor;
    
    //    self.view.frame = CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth2, CGRectGetHeight(self.view.bounds));
    UIView *menuTopView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth2, CGRectGetHeight(self.view.bounds)/4)];
    UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, menuTopView.width, CGRectGetHeight(self.view.bounds)/4)];
    bg.image = [UIImage imageNamed:@"ZhongxinBj.png"];
    [menuTopView addSubview:bg];
    
    
    self.headImgBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.width-[UIView getWidth:60.0f])/2, [UIView getHeight:30.0f], [UIView getWidth:60.0f] , [UIView getWidth:60.0f])];
    self.headImgBtn.layer.cornerRadius = CGRectGetHeight(self.headImgBtn.bounds)/2;
    //    self.headImgBtn.layer.masksToBounds = CGRectGetHeight(self.headImgBtn.bounds)/2;//圆形
    self.headImgBtn.layer.masksToBounds = YES;//圆形
    self.headImgBtn.layer.borderWidth = 3.0;
    self.headImgBtn.layer.borderColor = [YELLOWCOLOR CGColor];
    [self.headImgBtn setBackgroundImage:[UIImage imageNamed:@"headImg.png"] forState:UIControlStateNormal];
    //    [self.headImgBtn addTarget:self action:@selector(showSelectedImg:) forControlEvents:UIControlEventTouchUpInside];
    
    self.workNumLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.width-[UIView getWidth:100.0f])/2, self.headImgBtn.maxY+[UIView getHeight:10.0f] , [UIView getWidth:100.0f] , [UIView getHeight:15.0f])];
    self.workNumLbl.text = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]];
    self.workNumLbl.textColor = [UIColor whiteColor];
    self.workNumLbl.font = [UIView getFontWithSize:12];
    self.workNumLbl.textAlignment = NSTextAlignmentCenter;
    
    self.logoutBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.width-[UIView getWidth:30.0f], self.headImgBtn.maxY+[UIView getWidth:8.0f], [UIView getWidth:20.0f] , [UIView getWidth:20.0f])];
    [self.logoutBtn setBackgroundImage:[UIImage imageNamed:@"Btn_Normal_Zhuxiao.png"] forState:UIControlStateNormal];
    [self.logoutBtn addTarget:self action:@selector(logout:) forControlEvents:UIControlEventTouchUpInside];
    
    menuTopView.height = self.logoutBtn.maxY + [UIView getHeight:10.0f];
    bg.height = menuTopView.height;
    
    [self.view addSubview:menuTopView];
    [self.view addSubview:self.headImgBtn];
    [self.view addSubview:self.workNumLbl];
//    [self.view addSubview:self.logoutBtn];
    
    self.menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, menuTopView.height, self.view.width, self.view.height-menuTopView.height)];
    self.menuTableView.delegate = self;
    self.menuTableView.dataSource = self;
    self.menuTableView.backgroundColor = [UIColor clearColor];
    self.menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
    self.menuTableView.scrollEnabled =NO; //设置tableview 不能滚动
    [self.view addSubview:self.menuTableView];
}

- (void)logout:(UIButton *)button{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:@"roleId"];
    [userDefaults removeObjectForKey:@"roleName"];
    [userDefaults removeObjectForKey:@"roleTag"];
    [userDefaults removeObjectForKey:@"userId"];
    [userDefaults removeObjectForKey:@"userName"];
//    [userDefaults removeObjectForKey:@"userToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [dictionary setObject:@"app.user.login.out" forKey:@"method"];
    [dictionary setObject:@"yeguan" forKey:@"app_key"];
    [dictionary setObject:@"json" forKey:@"format"];
    [dictionary setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"userToken"] forKey:@"userToken"];
    [DataTool postWithUrl:HEAD_URL parameters:[DataTool addSignToDic:dictionary] success:^(id data) {
//        data =     {
//            update = SUCCESS;
//        };
//        status = SUCCESS;
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSString *status = [(NSDictionary *)jsonDic objectForKey:@"status"];
        NSLog(@"logout:%@",jsonDic);
        if ([status isEqualToString:@"SUCCESS"]) {
            [userDefaults removeObjectForKey:@"userToken"];
            ViewController *viewController = [[ViewController alloc] init];
            [self presentViewController:viewController animated:YES completion:nil];
        }else{
//            NSString *code = [(NSDictionary *)jsonDic objectForKey:@"code"];
//            NSString *msg = [(NSDictionary *)jsonDic objectForKey:@"msg"];
//            NSLog(@"%@%@%@",status,code,msg);
            [Tools showMessage:@"登出失败" view:self];
        }
    } fail:^(NSError *error) {
        NSLog(@"error:%@",error);
        [Tools showMessage:@"登出失败" view:self];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    // 设置自身窗口尺寸
    self.view.frame = CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth2, CGRectGetHeight(self.view.bounds));
}


-(void)takePhoto{
    //拍照，会自动回调- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info，对于自定义照相机界面，拍照后回调可以不退出实现连续拍照效果
    NSLog(@"takePicture...");
    [self.imagePikerViewController takePicture];
    
    [SVProgressHUD showWithStatus:@"识别中"];
//    self.takeButton.hidden = YES;
    self.takeButton.alpha = 0.5;
    self.takeButton.userInteractionEnabled = NO;
}

- (UIView *)drawCameraView {
    
    UIImage *bg_img = [UIImage imageNamed:@"Btn_Normal_Dier.png"];
    UIColor *lineColor = UIColorFromRGB(0xfdc500);
    
    CGFloat upheight = [UIView getWidth:20];
    CGFloat leftWidth = [UIView getWidth:20];
    CGFloat bottomheight = [UIView getWidth:60];
    CGFloat buttonsize = [UIView getWidth:40];
    
    UIView* cameraView = [[UIView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight)];
    
    UIView* upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, upheight)];
    upView.backgroundColor = [UIColor colorWithPatternImage:bg_img];
    [cameraView addSubview:upView];
    
    UIView* leftView = [[UIView alloc] initWithFrame:CGRectMake(0, upheight, leftWidth, kScreenHeight-bottomheight-upheight*2)];
    leftView.backgroundColor = [UIColor colorWithPatternImage:bg_img];
    [cameraView addSubview:leftView];
    
    UIView* rightView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth-leftWidth, leftView.y, leftView.width, leftView.height)];
    rightView.backgroundColor = [UIColor colorWithPatternImage:bg_img];
    [cameraView addSubview:rightView];
    
    self.middleView = [[UIImageView alloc] initWithFrame:CGRectMake(leftWidth, upView.maxY, kScreenWidth-leftView.width*2, kScreenHeight-bottomheight-upheight*2)];
    
//    middleView.backgroundColor = [UIColor redColor];
    self.middleView.alpha = 1;
    [cameraView addSubview:self.middleView];
    
    
    
    UIView * downView1 = [[UIView alloc] initWithFrame:CGRectMake(0, self.middleView.maxY, kScreenWidth, upView.height)];
    downView1.backgroundColor = [UIColor colorWithPatternImage:bg_img];
    [cameraView addSubview:downView1];
    
    UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight-bottomheight, kScreenWidth, bottomheight)];
    downView.alpha = 1;
    UIImage *bg_img2 = [UIImage imageNamed:@"Btn_Normal_Diyi.png"];
    downView.backgroundColor = [UIColor colorWithPatternImage:bg_img2];
    [cameraView addSubview:downView];
    
    self.takeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.takeButton.alpha = 1;

    UIImage *img = [UIImage imageNamed:@"Btn_Normal_TakeBtn.png"];
    [self.takeButton setImage:img forState:UIControlStateNormal];
    [self.takeButton setFrame:CGRectMake(kScreenWidth/2-buttonsize/2, downView.y+(bottomheight-buttonsize)/2, buttonsize, buttonsize)];
    [self.takeButton addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
    [cameraView addSubview:self.takeButton];
    
    
    //用于取消操作的button
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.alpha = 1;
    [cancelButton setFrame:CGRectMake(self.takeButton.maxX+50, downView.y+(bottomheight-buttonsize)/2, buttonsize, buttonsize)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:17]];
    [cancelButton addTarget:self action:@selector(cancelCamera) forControlEvents:UIControlEventTouchUpInside];
    [cameraView addSubview:cancelButton];
    

    CGFloat lineWidth = [UIView getWidth:4];
    CGFloat lineHeight = [UIView getWidth:16];
    
    UIView* leftUpLine = [[UIView alloc] initWithFrame:CGRectMake(leftWidth, upheight, lineWidth, lineHeight)];
    leftUpLine.alpha = 1;
    leftUpLine.backgroundColor = lineColor;
    [cameraView addSubview:leftUpLine];
    
    UIView* leftUpLine2 = [[UIView alloc] initWithFrame:CGRectMake(leftUpLine.x, leftView.y, leftUpLine.height, leftUpLine.width)];
    leftUpLine2.alpha = 1;
    leftUpLine2.backgroundColor = lineColor;
    [cameraView addSubview:leftUpLine2];
    
    UIView* rightUpLine = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth-leftWidth-lineWidth, leftView.y, leftUpLine.width, leftUpLine.height)];
    rightUpLine.alpha = 1;
    rightUpLine.backgroundColor = lineColor;
    [cameraView addSubview:rightUpLine];
    
    UIView* rightUpLine2 = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth-lineHeight-upheight, leftView.y, leftUpLine.height, leftUpLine.width)];
    rightUpLine2.alpha = 1;
    rightUpLine2.backgroundColor = lineColor;
    [cameraView addSubview:rightUpLine2];
    
    UIView* leftButLine = [[UIView alloc] initWithFrame:CGRectMake(leftUpLine.x, self.middleView.maxY-lineHeight, leftUpLine.width, leftUpLine.height)];
    leftButLine.alpha = 1;
    leftButLine.backgroundColor = lineColor;
    [cameraView addSubview:leftButLine];
    
    UIView* leftButLine2 = [[UIView alloc] initWithFrame:CGRectMake(leftUpLine.x, self.middleView.maxY-lineWidth, leftUpLine.height, leftUpLine.width)];
    leftButLine2.alpha = 1;
    leftButLine2.backgroundColor = lineColor;
    [cameraView addSubview:leftButLine2];
    
    UIView* rightButLine = [[UIView alloc] initWithFrame:CGRectMake(rightUpLine.x, leftButLine.y, leftUpLine.width, leftUpLine.height)];
    rightButLine.alpha = 1;
    rightButLine.backgroundColor = lineColor;
    [cameraView addSubview:rightButLine];
    
    UIView* rightButLine2 = [[UIView alloc] initWithFrame:CGRectMake(rightUpLine2.x, leftButLine2.y, leftUpLine.height, leftUpLine.width)];
    rightButLine2.alpha = 1;
    rightButLine2.backgroundColor = lineColor;
    [cameraView addSubview:rightButLine2];
    
    UIView* borderLine1 = [[UIView alloc] initWithFrame:CGRectMake(leftUpLine.x, leftView.y, self.middleView.width, 1)];
    borderLine1.alpha = 1;
    borderLine1.backgroundColor = lineColor;
    [cameraView addSubview:borderLine1];
    UIView* borderLine2 = [[UIView alloc] initWithFrame:CGRectMake(borderLine1.x, rightButLine2.maxY-1, borderLine1.width, 1)];
    borderLine2.alpha = 1;
    borderLine2.backgroundColor = lineColor;
    [cameraView addSubview:borderLine2];
    UIView* borderLine3 = [[UIView alloc] initWithFrame:CGRectMake(leftUpLine.x, leftUpLine.y, 1, self.middleView.height)];
    borderLine3.alpha = 1;
    borderLine3.backgroundColor = lineColor;
    [cameraView addSubview:borderLine3];
    UIView* borderLine4 = [[UIView alloc] initWithFrame:CGRectMake(rightUpLine.maxX-1, borderLine3.y, borderLine3.width, borderLine3.height)];
    borderLine4.alpha = 1;
    borderLine4.backgroundColor = lineColor;
    [cameraView addSubview:borderLine4];
    
    return cameraView;
    
}


//startScanning
- (void)startScanning{

    if(![Tools isAllowTakePhoto]){
        [Tools showMessage:@"APP无相机访问权限，请先设置!" view:self];
        return;
    }
    
//    if(![Tools isAllowPhoto]){
//        [Tools showMessage:@"APP无相册访问权限，请先设置!" view:self];
//        return;
//    }
    self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.imagePikerViewController.delegate = self;
    self.imagePikerViewController.showsCameraControls = NO;
    self.overlayView.frame = self.imagePikerViewController.cameraOverlayView.frame;
    self.overlayView.backgroundColor = [UIColor clearColor];
    self.imagePikerViewController.cameraOverlayView = [self drawCameraView];
    
    // 相机全屏
    CGSize screenBounds = [UIScreen mainScreen].bounds.size;
    CGFloat cameraAspectRatio = 4.0f/3.0f;
    CGFloat camViewHeight = screenBounds.width * cameraAspectRatio;
    CGFloat scale = screenBounds.height / camViewHeight;
    self.imagePikerViewController.cameraViewTransform = CGAffineTransformMakeTranslation(0, (screenBounds.height - camViewHeight) / 2.0);
    self.imagePikerViewController.cameraViewTransform = CGAffineTransformScale(self.imagePikerViewController.cameraViewTransform, scale, scale);
    
    [self presentViewController:self.imagePikerViewController animated:YES completion:NULL];
}

#pragma mark- 缩放图片
-(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
    UIGraphicsBeginImageContext(CGSizeMake(self.middleView.width,(image.size.width/image.size.height)*self.middleView.height));
    [image drawInRect:CGRectMake(0, 0, self.middleView.width,(image.size.width/image.size.height)*self.middleView.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

#pragma mark -
#pragma mark - UIImagePickerControllerDelegate

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//    UIImage * image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
//    self.middleView.image = image;
//    [self scaleImage:image toScale:0.8];
//    self.middleView.image = [self scaleImage:image toScale:0.6];
//    return;
    HWCloudsdk *sdk = [[HWCloudsdk alloc] init];
    
    NSString *apiKey = @"28b2f9d7-2832-479c-a3e9-480c74508eae";
    NSLog(@"识别开始。。。");
    [sdk cardLanguage:@"chns" cardImage:image apiKey:apiKey successBlock:^(id responseObject) {
        [self cancelCamera];
        NSLog(@"%@",responseObject);
        NSData *data = [responseObject dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        NSDictionary * rootDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
//        NSMutableString *reString = [NSMutableString string];
//        [reString appendString:@"{"];
        NSString *code = [rootDic objectForKey:@"code"];
        NSArray *comp = [rootDic objectForKey:@"comp"];
        NSArray *name = [rootDic objectForKey:@"name"];
        if ([code isEqualToString:@"0"]) {
            [SVProgressHUD dismiss];
            if (comp.count > 0 | name.count > 0) {
                NSLog(@"扫描成功:%@。%@。%@。",code,comp,name);
                proVC.cardDictionary = rootDic;
                [self onSelectRowAtIndexPathRow:1 hideSidebar:YES];
            }else{
                [SVProgressHUD showErrorWithStatus:@"网络异常或超时,请稍后再试！"];
            }
        }else{
            NSString *result = [rootDic objectForKey:@"result"];
            NSLog(@"识别失败：%@",result);
            [SVProgressHUD dismiss];
            [SVProgressHUD showErrorWithStatus:@"网络异常或超时,请稍后再试！"];
//            [SVProgressHUD dismissWithDelay:1555.0];
        }
        
    } failureBlock:^(NSError *error) {
        NSLog(@"识别fail：%@",error);
        [SVProgressHUD showErrorWithStatus:@"识别失败！"];
    }];
}

-(void)cancelCamera{
    [self.imagePikerViewController dismissViewControllerAnimated:YES completion:nil];
}

// 处理菜单项点击事件.
- (BOOL)onSelectRowAtIndexPathRow:(int)row hideSidebar:(BOOL)hideSidebar {
    BOOL rt = NO;
    do {
//        if (nil==row) break;
        // 获得当前项目.
        id controller = _controllers[row];
        if (nil!=controller) {
            // 命令.
            if ([controller isKindOfClass:NSString.class]) {
                NSString* cmd = controller;
                if ([cmd isEqualToString:@"scanning"]) {
                    [self startScanning];
                    rt = YES;
                    break;
                }
            }
            
            // 页面跳转.
            if ([controller isKindOfClass:UIViewController.class]) {
                rt = YES;
                revealController.contentViewController = controller;
                if (hideSidebar) {
                    [revealController toggleSidebar:NO duration:kGHRevealSidebarDefaultAnimationDuration];
                }
            }
        }
    } while (0);
    return rt;
}

/// 选择某个菜单项.
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition {
    [_menuTableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
    if (scrollPosition == UITableViewScrollPositionNone) {
        [_menuTableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
    }

    [self onSelectRowAtIndexPathRow:(int)indexPath.row hideSidebar:NO];
    NSLog(@"selectRowAtIndexPath: %@", revealController.contentViewController);
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _cellInfos.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"MenuListCell";
    MenuListCell *cell = (MenuListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MenuListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    NSDictionary *info = _cellInfos[indexPath.row];
    cell.defaultImage = info[kSidebarCellImageKey];
    cell.selectedImage = info[kSidebarCellSelectImageKey];
    cell.textLabel.text = info[kSidebarCellTextKey];
    cell.textLabel.font = [ViewTool getFont:16.0f];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UIView getHeight:40.0f];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return [UIView getHeight:30.0f];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.menuTableView.frame.size.width, [UIView getHeight:30.0f])];
    view.backgroundColor = [UIColor clearColor];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake([UIView getWidth:10.0f], [UIView getHeight:5.0f], self.menuTableView.frame.size.width-[UIView getWidth:10.0f]*2, [UIView getHeight:20.0f])];
//    lab.text = [self.indexArray objectAtIndex:section];
    lab.text = @"MENU";
    lab.font = [ViewTool getFont:13.0f];
    lab.textColor = [UIColor whiteColor];
    [view addSubview:lab];
    
    UIView *lineView = [ViewTool getLineViewWith:CGRectMake(0, [UIView getHeight:30.0f]-1, self.menuTableView.frame.size.width, 1) withBackgroudColor:UIColorFromRGB(0x0f4488)];
    [view addSubview:lineView];
    
    return view;
}

/// 处理单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self onSelectRowAtIndexPathRow:(int)indexPath.row hideSidebar:YES];
    NSLog(@"didSelectRowAtIndexPath: %@", revealController.contentViewController);
}

/// 取消按钮:点击.
- (void)cancelButton_selector:(id)sender {
    if (nil!=revealController) {
        [revealController dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
